package com.example.myspringai.tangguomeng;

import com.google.gson.JsonArray;
import com.google.gson.JsonObject;
import com.google.gson.JsonParser;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.entity.StringEntity;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.util.EntityUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.nio.charset.StandardCharsets;

public class OpenAIByTang {

    private static final Logger log = LoggerFactory.getLogger(OpenAIByTang.class);

    public static void main(String[] args) {
        System.out.println(message("帮我设计一张mysql表，表明为llama_guard_classify,包括字段text,classify,create_time,update_time"));
    }

    public static String message(String message) {
        // 创建HttpClient对象
        try (CloseableHttpClient httpClient = HttpClients.createDefault()) {
            // 创建POST请求
            HttpPost postRequest = new HttpPost("https://bot.tgmeng.com/adminapi/chatapi/chat/message");
            handleThis(postRequest);
            // 设置请求体
            String json = "{\"topicId\": 18261529934149, \"content\": \"" + message + "\", \"contentFiles\": []}";
            System.out.println("Request body: " + json); // 调试日志
            StringEntity entity = new StringEntity(json, StandardCharsets.UTF_8); // 确保字符编码正确
            postRequest.setEntity(entity);
            postRequest.setHeader("Content-Type", "application/json; charset=UTF-8");

            // 发送请求
            try (CloseableHttpResponse response = httpClient.execute(postRequest)) {
                // 获取响应状态码
                int statusCode = response.getStatusLine().getStatusCode();
                System.out.println("Status code: " + statusCode);

                // 获取响应内容
                String responseBody = EntityUtils.toString(response.getEntity(), StandardCharsets.UTF_8); // 确保字符编码正确
                System.out.println("Response body: " + responseBody);
                // 使用Gson解析响应内容
                JsonObject jsonObject = JsonParser.parseString(responseBody).getAsJsonObject();
                JsonArray resultArray = jsonObject.getAsJsonArray("result");
                long secondResult = resultArray.get(1).getAsLong();
                System.out.println("Second result: " + secondResult);

                HttpPost postRequest_2 = new HttpPost("https://bot.tgmeng.com/adminapi/chatapi/chat/message/" + secondResult);
                postRequest_2.setHeader("Content-Type", "application/json; charset=UTF-8");
                handleThis(postRequest_2);

                // 发送请求
                try (CloseableHttpResponse response2 = httpClient.execute(postRequest_2)) {
                    // 获取响应状态码
                    int statusCode2 = response2.getStatusLine().getStatusCode();
                    System.out.println("Status code: " + statusCode2);

                    // 获取响应内容
                    String responseBody2 = EntityUtils.toString(response2.getEntity(), StandardCharsets.UTF_8); // 确保字符编码正确
                    System.out.println("Response body: " + responseBody2);
                    return responseBody2;
                }
            }catch (Exception e){
                log.error("发送请求失败", e);
                return "";
            }
        } catch (Exception e) {
            log.error("发送请求失败", e);
            return "";
        }
    }

    private static void handleThis(HttpPost postRequest) {
        // 设置Header
        postRequest.setHeader("authority", "bot.tgmeng.com");
        postRequest.setHeader("accept", "application/json, text/plain, */*");
        postRequest.setHeader("accept-language", "zh-CN");
        postRequest.setHeader("authorization", "\n" +
                "Bearer eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJNZW1iZXJJZCI6MTU3NzM1MDY1NzQyNzcsIkFjY291bnRUeXBlIjoxLCJOaWNrTmFtZSI6Ik1fMVhZY0I4SHRWRUQ2IiwiQWNjb3VudCI6IjEzMTYzMjM0NjUxIiwiTG9naW5Nb2RlIjoxLCJpYXQiOjE3MjgzNzE5NjIsIm5iZiI6MTcyODM3MTk2MiwiZXhwIjoxNzI5NTgxNTYyLCJpc3MiOiJBSVRvb2xzIiwiYXVkIjoiQUlUb29scyJ9.LZyS5gYIfB1mpnhjxSRIngQCaniFptf-buo4TGamJzc" );
        postRequest.setHeader("content-type", "application/json");
        postRequest.setHeader("origin", "https://bot.tgmeng.com");
        postRequest.setHeader("sec-ch-ua", "\"Not_A Brand\";v=\"8\", \"Chromium\";v=\"120\", \"Microsoft Edge\";v=\"120\"");
        postRequest.setHeader("sec-ch-ua-mobile", "?0");
        postRequest.setHeader("sec-ch-ua-platform", "\"Windows\"");
        postRequest.setHeader("sec-fetch-dest", "empty");
        postRequest.setHeader("sec-fetch-mode", "cors");
        postRequest.setHeader("sec-fetch-site", "same-origin");
        postRequest.setHeader("user-agent", "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/120.0.0.0 Safari/537.36 Edg/120.0.0.0");
    }
}
